package com.ellecity06.common.widgets.decoration

import android.annotation.SuppressLint
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

import androidx.recyclerview.widget.GridLayoutManager
import com.ellecity06.common.R
import com.ellecity06.common.utils.appContext


/**
 * @author zavier
 * @time 2023/4/17 15:56
 * @des 网格分割线
 */
@SuppressLint("UseCompatLoadingForDrawables")
class NormalGridDecoration constructor(
    @DimenRes size: Int = R.dimen.dp_10,
    @ColorRes color: Int = R.color.transparent
) : RecyclerView.ItemDecoration() {

    // 分割线的高度或宽度
    private var dividerSize = -1

    // 分割线颜色
    private var divider: Drawable

    init {
        dividerSize = appContext.resources.getDimensionPixelSize(size)
        divider = appContext.resources.getDrawable(
            color,
            appContext.theme
        )
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        // 画横线
        drawHorizontalLine(canvas, parent)
        // 画竖线
        drawVerticalLine(canvas, parent)
    }

    /**
     * 画横线
     */
    private fun drawHorizontalLine(canvas: Canvas, parent: RecyclerView) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val bottomLeft = child.left - params.leftMargin - dividerSize
            val bottomRight = child.right + params.rightMargin + dividerSize
            val bottomTop = child.bottom + params.bottomMargin
            val bottomBottom = bottomTop + dividerSize
            divider.setBounds(bottomLeft, bottomTop, bottomRight, bottomBottom)
            divider.draw(canvas)
        }
    }

    /**
     * 画竖线
     */
    private fun drawVerticalLine(canvas: Canvas, parent: RecyclerView) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val rightTop = child.top - params.topMargin
            val rightBottom = child.bottom + params.bottomMargin + dividerSize
            val rightLeft = child.right + params.rightMargin
            val rightRight = rightLeft + dividerSize
            divider.setBounds(rightLeft, rightTop, rightRight, rightBottom)
            divider.draw(canvas)
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val spanCount: Int = getSpanCount(parent)
        var itemPosition = parent.getChildAdapterPosition(view)
        itemPosition += 1

        val leftDivider: Int
        val rightDivider: Int
        val bottomDivider = dividerSize
        when {
            (itemPosition - 1) % spanCount == 0 -> { // 第一条线
                leftDivider = 0
                rightDivider = (dividerSize * ((spanCount - 1.0) / spanCount)).toInt()
            }

            itemPosition % spanCount == 0 -> { // 最后一条线
                rightDivider = 0
                leftDivider = (dividerSize * ((spanCount - 1.0) / spanCount)).toInt()
            }

            else -> {
                leftDivider = (dividerSize * ((itemPosition % spanCount - 1.0) / spanCount)).toInt()
                rightDivider =
                    (dividerSize * ((spanCount - itemPosition.toDouble() % spanCount) / spanCount)).toInt()
            }
        }

        outRect[leftDivider, 0, rightDivider] = bottomDivider
    }

    private fun getSpanCount(parent: RecyclerView): Int {
        var spanCount = -1
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            spanCount = layoutManager.spanCount
        } else if (layoutManager is StaggeredGridLayoutManager) {
            spanCount = layoutManager.spanCount
        }
        return spanCount
    }
}